package boot.service.impl.wxUser;


import boot.dao.WxUserMapper;
import boot.domain.wxUser.WxUser;
import boot.domain.wxUser.WxUserExample;
import boot.service.wxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author cb
 * @create 2022-09-13-19:43
 */
@Service
public class wxUserServiceImpl implements wxUserService {
    @Autowired
    private WxUserMapper wxUserMapper;

    @Override
    public boolean addUser(WxUser user) {
        WxUserExample wxUserExample = new WxUserExample();
        WxUserExample.Criteria criteria = wxUserExample.createCriteria();
        criteria.andOpenidEqualTo(user.getOpenid());
        if(wxUserMapper.selectByExample(wxUserExample).size()==0){
            try {
                wxUserMapper.insert(user);
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return  false;
            }
        }
        return true;
    }

    @Override
    public int getIdByOpenId(String openId) {
        WxUserExample wxUserExample = new WxUserExample();
        WxUserExample.Criteria criteria = wxUserExample.createCriteria();
        criteria.andOpenidEqualTo(openId);
        if(wxUserMapper.selectByExample(wxUserExample).size()!=0){
            return wxUserMapper.selectByExample(wxUserExample).get(0).getId();
        }else {
            return 0;
        }
    }
}
